package main

import "fmt"

func t_variable_parameter() {
	var v1 = 1
	var (
		v2 = 234
		v3 = "hello"
		v4 = 1.23
	)
	MyPrintf(v1,v2,v3,v4)

	sum := func(nums ...int) int {
		res := 0
		for _, n := range nums {
			res += n
		}
		return res
	}
	fmt.Println("sum()=", sum())
	fmt.Println("sum()=", sum(1,2))

	sl := []int{1,2,3}
	fmt.Println(sum(sl...)) // pass slice to a varidic function, which need unpack the slice first

	ar := [...]string{"a", "b"} // 表示一个长度值，等于数组元素的个数
	fmt.Println(len(ar))
}

//code snippet 2-4 in book
func MyPrintf(args ...interface{}) {
	for _,arg := range args {
		switch arg.(type) { //这种写法很奇怪
		case int:
			fmt.Println(arg,"is an int")
		case string:
			fmt.Println(arg , "is a string")
		default:
			fmt.Println(arg,"is an unknown type")
		}
	}
}

func main() {

}
